package com.cqust.wdm.emsboot.controller;

import com.cqust.wdm.emsboot.domain.Product;
import com.cqust.wdm.emsboot.service.IProductService;
import com.cqust.wdm.emsboot.util.AjaxResult;
import com.cqust.wdm.emsboot.util.BaseController;
import com.cqust.wdm.emsboot.util.ExcelUtil;
import com.cqust.wdm.emsboot.util.page.TableDataInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 商品Controller
 */
@RestController
@RequestMapping("/emsboot/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    /**
     * 查询商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    @GetMapping("/export")
    public AjaxResult export(Product product) {
        startPage();
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        return util.exportExcel(list, "商品信息");
    }

    /**
     * 获取商品详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productService.selectProductById(id));
    }

    /**
     * 新增商品
     */
    @ApiOperation("新增商品")
    @PostMapping
    public AjaxResult add(@RequestBody Product product) {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改商品
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }
}
